<template>
  <div class="journalism-container">
    <el-card class="filter-card fixed-filter">
      <div class="filter-row">
        <label class="filter-label">新闻内容：</label>
        <el-input
          v-model="searchText"
          placeholder="请输入新闻内容或标题"
          clearable
          style="width: 300px"
          @keyup.enter="handleSearch"
        />
      </div>

      <div class="filter-row">
        <span class="filter-label">新闻类型：</span>
        <div class="tag-group">
          <el-tag
            v-for="tag in typeTags"
            :key="tag.label"
            v-bind="getTagProps(tag)"
            :effect="selectedType === tag.label ? 'dark' : 'plain'"
            class="tag-item"
            @click="selectType(tag.label)"
            size="large"
          >
            {{ tag.label }}
          </el-tag>
        </div>
      </div>

      <div class="filter-row">
        <span class="filter-label">筛选条件：</span>
        <div class="condition-group">
          <shaixuan
            v-for="item in conditions"
            :key="item"
            :title="item"
            @condition-change="handleConditionChange(item, $event)"
          />
        </div>
      </div>
    </el-card>
  </div>
  <div class="news-card">
    <newsCard :filters="filters" />  
  </div>
 
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import shaixuan from '@/components/JournaLism/shaixuan.vue'
import newsCard from '@/components/JournaLism/newsCard.vue'

const searchText = ref('')
const selectedType = ref('全部')

const filterConditions = reactive({
  '时间降序': '',
  '时间范围': '',
  '添加方式': '',
  '发布状态': ''
})

const filters = reactive({
  searchText: '',
  newsType: '全部',
  sortBy: '时间降序',
  timeRange: '',
  addMethod: '',
  publishStatus: ''
})

const typeTags = [
  { label: '全部' },
  { label: '宏观形式' },
  { label: '行业动态' },
  { label: '产业链动态' },
  { label: '国际能源观察' },
  { label: '其他' },
]

const conditions = ['时间降序', '时间范围', '添加方式', '发布状态']

const getTagProps = (tag) => {
  const props = {}
  if (tag.type && tag.type.trim() !== '') {
    props.type = tag.type
  }
  return props
}

const selectType = (type) => {
  selectedType.value = type
  filters.newsType = type
}

const handleConditionChange = (condition, value) => {
  filterConditions[condition] = value
  

  if (condition.includes('时间降序')) {
    filters.sortBy = value || '时间降序'
  } else if (condition.includes('时间范围')) {
    filters.timeRange = value
  } else if (condition.includes('添加方式')) {
    filters.addMethod = value
  } else if (condition.includes('发布状态')) {
    filters.publishStatus = value
  }
}

const handleSearch = () => {
  filters.searchText = searchText.value
}

watch(searchText, (newVal) => {
  filters.searchText = newVal
})

filters.newsType = selectedType.value
filters.sortBy = '时间降序'
</script>

<style scoped>

.filter-card {
  margin-top: 20px;
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  background-color: #fff;
}

.fixed-filter {
  position: sticky;
  top: 20px;
  z-index: 100;
}
.filter-row {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.filter-label {
  width: 90px;
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.tag-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: 1;
}

.tag-item {
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 6px;
  padding: 5px 10px;
}

.tag-item:hover {
  transform: scale(1.05);
}

.condition-group {
  display: flex;
  gap: 12px;
  flex: 1;
  flex-wrap: wrap;
}

.news-card {
  margin-top: 20px;
  max-height: calc(100vh - 320px);
  overflow: auto;
}
.news-card ::-webkit-scrollbar {
  display: none;
}
</style>
